<?php
declare (strict_types = 1);

namespace app\admin\model\content;

use think\Model;

/**
 * @mixin \think\Model
 */
class Advert extends Model
{
    protected $pk = "id";

    /**
     * 获取信息
     * @param $id
     * @return array|null|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function getInfoById($id){
        return $this->where('id',$id)->find();
    }


    /**
     * 列表
     * @param $getData
     * @param $page
     * @param $limit
     * @param $where
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function getList($getData,$page,$limit,$where = []){
        $sql = $this->where($where);
        if(isset($getData['title']) && $getData['title']) $sql = $sql->whereLike('title','%'.$getData['title'].'%');
        $count = $sql->count();
        $statusType = [1=>'显示',0=>'隐藏'];
        $data = $sql->page($page,$limit)->order('id','desc')->select()->each(function ($item)use($statusType){
            $item['status_name'] = $statusType[$item['is_status']];
            return $item;
        })->toArray();
        return compact('count','data');
    }

    /**
     *  添加数据
     * @param $addData
     * @return int|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function addData($addData){
        return $this->insertGetId([
            'title'=>$addData['title'],
            'position_id'=>isset($addData['position_id']) ? $addData['position_id'] : 2,
            'platform_id'=>isset($addData['platform_id']) ? $addData['platform_id'] : 2,
            'image'=>$addData['image'],
            'is_status'=>$addData['is_status'],
            'url'=>$addData['url'],
            'sort'=>$addData['sort'] ? $addData['sort'] : 0,
            'remark'=>$addData['remark'],
            'create_time'=>date("Y-m-d H:i:s"),
            'update_time'=>date("Y-m-d H:i:s"),
        ]);
    }

    /**
     * 修改
     * @param $saveData
     * @return static
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function editData($saveData){
        $updateData = [
            'title'=>$saveData['title'],
            'image'=>$saveData['image'],
            'is_status'=>$saveData['is_status'],
            'url'=>$saveData['url'],
            'sort'=>$saveData['sort'] ? $saveData['sort'] : 0,
            'remark'=>$saveData['remark'],
            'update_time'=>date("Y-m-d H:i:s"),
        ];
        return $this->where('id',$saveData['id'])->update($updateData);
    }

    /**
     * 删除
     * @param $id
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function delData($id){
        $info = $this->where('id',$id)->find();
        if(!$info) return ['code'=>0,'msg'=>'删除成功'];
        $result = $this->where('id',$id)->delete();
        if($result) return ['code'=>0,'msg'=>'删除成功'];
        return ['code'=>1,'msg'=>'删除失败，请稍后重试'];

    }

    /**
     * 获取指定内容
     * @param $where
     * @param string $field
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function getAllList($where,$field='*'){
        return $this->where($where)->field($field)->select()->toArray();
    }

}
